package com.bishe.bishe.Controller;

import com.bishe.bishe.Service.UserService;

import com.bishe.bishe.entity.PageResult;
import com.bishe.bishe.pojo.Comments;
import com.bishe.bishe.pojo.User;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public User login(@RequestBody User user )throws Exception{
       User user1=userService.login(user);

       if (user1!=null&&user1.getUserName()!=null){
           return user1;
       }else {
           return null;
       }
    }

    @RequestMapping("/insert")
    public Integer insert(@RequestBody User user  )throws Exception {
        User checkuser = checkuser(user.getUserEntername());
        if (checkuser==null){
            return userService.insert(user);
        }else {
            return 0;
        }
    }
    @RequestMapping("/delete")
    public int delete(@Validated @RequestParam(value = "userId",required = false) int userId ){
        return userService.delete(userId);
    }
    @RequestMapping("/update")
    @ResponseBody
    public int update(@RequestBody  User user){
        return userService.update(user);
    }

    @RequestMapping("/finduser")
    @ResponseBody
    public User finduser(@Validated @RequestParam(value = "userId",required = false)int userId){
        return userService.finduser(userId);
    }

    @RequestMapping("/findall")
    public List<User> findall(){
        return userService.findall();
    }

    public User checkuser(String userEntername){
        return userService.checkuser(userEntername);
    }

}
